// @algorithm @lc id=238 lang=cpp 
// @title product-of-array-except-self


// @test([1,2,3,4])=[24,12,8,6]
// @test([-1,1,0,-3,3])=[0,0,9,0,0]
class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> ans(n, 1);
        for(int i=1; i<n; i++)
            ans[i] = ans[i-1] * nums[i-1];
        int R = 1;
        for(int i=n-1; i>=0; i--){
            ans[i] *= R;
            R *= nums[i];
        }
        return ans;
    }
};